﻿// ———————————————————————–
// <copyright file="GMLMeasureType.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// GMLMeasureType.cs:
//
// gml:MeasureType supports recording an amount encoded as a value of XML Schema double, together with a units of
// measure indicated by an attribute uom, short for "units Of measure". The value of the uom attribute identifies
// a reference system for the amount, usually a ratio or interval scale.
//
// Project: IC.NET - GeoOASISWhereLib
// Author:  Jim Cronin - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Text.RegularExpressions;
using System.Xml;
using EDXLSharp;

namespace GeoOASISWhereLib
{
  /// <summary>
  /// gml:MeasureType supports recording an amount encoded as a value of XML Schema double, together with a units of
  /// measure indicated by an attribute uom, short for "units Of measure". The value of the uom attribute identifies
  /// a reference system for the amount, usually a ratio or interval scale.
  /// </summary>
  public abstract class GMLMeasureType : ComplexTypeBase
  {
    public string UnitOfMeasure;
    public double Value;

    public override void WriteXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      Validate();

      xwriter.WriteStartElement(ElementPrefix, ElementName, ElementNamespace);
      if (!string.IsNullOrEmpty(UnitOfMeasure))
      {
        xwriter.WriteAttributeString("uom", EDXLConstants.GMLNamespace, UnitOfMeasure);
      }
      xwriter.WriteString(Value.ToString());
      xwriter.WriteEndElement();
    }

    public override void ReadXML(XmlNode rootnode)
    {
      if (rootnode == null)
      {
        throw new ArgumentNullException("rootnode");
      }
      if (rootnode.LocalName != ElementName)
      {
        throw new ArgumentException("Unexpected element " + rootnode.LocalName + " instead of " + ElementName);
      }

      if (rootnode.Attributes["uom"] != null)
      {
        UnitOfMeasure = rootnode.Attributes["uom"].InnerText;
      }

      try
      {
        Value = double.Parse(rootnode.InnerText);
      }
      catch (Exception ex)
      {
        throw new ArgumentException("Failed to parse GMLMeasureType. See inner exception for details", ex);
      }

      Validate();
    }

    public override void Validate()
    {
      if (!string.IsNullOrEmpty(UnitOfMeasure))
      {
        if (!Regex.IsMatch(UnitOfMeasure, "[^: \n\r\t]+") && !Regex.IsMatch(UnitOfMeasure, @"([a-zA-Z][a-zA-Z0-9\-\+\.]*:|\.\./|\./|#).*"))
        {
          throw new ValidationException("Invalid format for Unit of Measure");
        }
      }
    }
  }
}